import 'package:faker/faker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:zootix/models/zt_rank_account.model.dart';
import 'package:zootix/screens/rank/zt_rank.controller.dart';
import 'package:zootix/util/zt_screen_adaper_util.dart';
import 'package:zootix/util/zt_snack_bar.util.dart';
import 'package:zootix/widgets/zt_empty.dart';
import 'package:zootix/widgets/zt_fade_in_cache_image.dart';
import 'package:zootix/zt_app_theme.dart';
import 'package:zootix/constants/zt_router.dart';

class ZtRankPage extends StatelessWidget {
  final _controller = Get.put<ZtRankController>(ZtRankController());
  late String title;
  ZtRankPage({super.key}) {
    title = Get.arguments['title'];
    _controller.parkType = title.toLowerCase();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/rank_bg.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          title: Text(title),
          backgroundColor: Colors.transparent,
          elevation: 0,
        ),
        body: _buildBody(),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        floatingActionButton: ElevatedButton(
          style: ElevatedButton.styleFrom(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(ZtScreenAdaper.sp(30))),
              padding: EdgeInsets.symmetric(
                  horizontal: ZtScreenAdaper.height(50),
                  vertical: ZtScreenAdaper.height(10)),
              backgroundColor: const Color(0xFFFFCA41)),
          onPressed: () {
            _controller.confirmBuyCoin();
          },
          child: Text(
            'Buy Ticket',
            style: TextStyle(fontSize: ZtScreenAdaper.sp(18)),
          ),
        ),
      ),
    );
  }

  Widget _buildBody() {
    return SafeArea(
        child: Obx(
      () => _controller.rankList.isEmpty
          ? ZtEmpty()
          : SingleChildScrollView(
              child: Column(
              children: [_buildTop3(), ..._buildNormalList()],
            )),
    ));
  }

  Widget _buildTop3() {
    return Row(
      children: List.generate(
        3,
        (index) {
          int realRankIndex = index == 0
              ? 1
              : index == 1
                  ? 0
                  : 2;
          return Container(
            height: ZtScreenAdaper.height(250),
            width: ZtScreenAdaper.screenWidth() / 3,
            child: Stack(
              alignment: Alignment.topCenter,
              children: [
                Positioned(
                    top: index == 1
                        ? ZtScreenAdaper.height(30)
                        : ZtScreenAdaper.height(60),
                    child: Image(
                        width: ZtScreenAdaper.screenWidth() / 3 -
                            ZtScreenAdaper.width(10),
                        image: AssetImage(
                            'assets/images/rank_top${realRankIndex + 1}_bg.png'))),
                Positioned(
                  top: index == 1
                      ? ZtScreenAdaper.height(12)
                      : ZtScreenAdaper.height(35),
                  child: Container(
                    height: ZtScreenAdaper.width(58),
                    width: ZtScreenAdaper.width(58),
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage(
                                _controller.rankList[realRankIndex].avatar ??
                                    '')),
                        border: Border.all(
                            color: ZtAppTheme.white,
                            width: ZtScreenAdaper.sp(2)),
                        borderRadius: BorderRadius.circular(200)),
                  ),
                ),
                Positioned(
                    top: index == 1
                        ? ZtScreenAdaper.height(0)
                        : ZtScreenAdaper.height(20),
                    child: InkWell(
                        onTap: () {
                          _controller.goPriviateChat(realRankIndex);
                        },
                        child: Image(
                            height: ZtScreenAdaper.height(100),
                            width: ZtScreenAdaper.width(100),
                            image: AssetImage(
                                'assets/images/rank_top${realRankIndex + 1}_avatar.png')))),
                Positioned(
                    top: index == 1
                        ? ZtScreenAdaper.height(90)
                        : ZtScreenAdaper.height(120),
                    child: InkWell(
                      onTap: () => {_controller.goPriviateChat(realRankIndex)},
                      child: Image(
                          width: ZtScreenAdaper.width(60),
                          image: AssetImage(
                              'assets/images/rank_top${realRankIndex + 1}_title.png')),
                    )),
                Positioned(
                    top: index == 1
                        ? ZtScreenAdaper.height(125)
                        : ZtScreenAdaper.height(150),
                    child: Container(
                      alignment: Alignment.center,
                      width: ZtScreenAdaper.screenWidth() / 3 -
                          ZtScreenAdaper.width(5),
                      child: Text(
                        '${_controller.rankList[realRankIndex].nickname}',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontWeight: FontWeight.w500,
                            overflow: TextOverflow.ellipsis,
                            fontSize: ZtScreenAdaper.sp(16)),
                      ),
                    )),
                Positioned(
                    right: ZtScreenAdaper.width(40),
                    top: index == 1
                        ? ZtScreenAdaper.height(55)
                        : ZtScreenAdaper.height(75),
                    child: buildFollowBtn(
                      realRankIndex,
                    )),
              ],
            ),
          );
        },
      ),
    );
  }

  List<Widget> _buildNormalList() {
    return _controller.rankList
        .skip(3)
        .map((e) => _buildNormalItem(e, _controller.rankList.indexOf(e)))
        .toList();
  }

  Widget _buildNormalItem(RankAccount account, int index) {
    return InkWell(
      child: Container(
        margin: EdgeInsets.symmetric(
            vertical: ZtScreenAdaper.height(10),
            horizontal: ZtScreenAdaper.width(10)),
        padding: EdgeInsets.only(
            left: ZtScreenAdaper.width(5), right: ZtScreenAdaper.width(20)),
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(10)),
        child: Row(mainAxisAlignment: MainAxisAlignment.start, children: [
          Stack(
            alignment: Alignment.center,
            children: [
              Container(
                clipBehavior: Clip.hardEdge,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                ),
                width: ZtScreenAdaper.width(50),
                child: ZtFadeInCacheImage(
                  width: ZtScreenAdaper.width(50),
                  url: account.avatar,
                ),
              ),
              Image(
                  height: ZtScreenAdaper.width(70),
                  image: const AssetImage(
                      'assets/images/rank_top_normal_avatar.png')),
              Positioned(
                  bottom: ZtScreenAdaper.height(5),
                  right: ZtScreenAdaper.width(5),
                  child: buildFollowBtn(index))
            ],
          ),
          SizedBox(
            width: ZtScreenAdaper.width(5),
          ),
          Expanded(
              child: Text(
            '${account.nickname}',
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: ZtScreenAdaper.sp(15), fontWeight: FontWeight.w500),
          )),
        ]),
      ),
      onTap: () {
        _controller.goPriviateChat(index);
      },
    );
  }

  Widget buildFollowBtn(int index) {
    if (!_controller.rankList[index].isFriend!) {
      return InkWell(
          onTap: () async {
            await _controller.addFriend(
                _controller.rankList[index].userId!, index);
          },
          child: Image(
              height: ZtScreenAdaper.width(25),
              image:
                  const AssetImage('assets/images/rank_account_follow.png')));
    } else {
      return const SizedBox();
    }
  }
}
